<?php
/**
 * 管理员管理
 * @author zzdboy
 *
 */
class AdministratorAction extends AdminAction {
	public function index() {
		$Admin = D ( "Admin" );
		
		// 导入分页类
		import ( "ORG.Util.Page" );
		$count = $Admin->where ()->count ();
		$Page = new Page ( $count, 100 );
		$show = $Page->show ();
		$list = $Admin->where ()->order ( "uid DESC" )->limit ( $Page->firstRow . ',' . $Page->listRows )->select ();
		
		if ($list) {
			foreach ( $list as &$val ) {
				$AdminRole = D ( "AdminRole" );
				$Role_info = $AdminRole->find ( $val ['roleid'] );
				$val ['rolename'] = $Role_info ['rolename'];
				$val ['lastlogintime'] = date ( "Y-m-d H:i:s", $val ['lastlogintime'] );
			}
		}
		$this->assign ( 'list', $list );
		$this->assign ( 'page', $show );
		$this->display ();
	}
	
	/**
	 * 添加
	 */
	public function add() {
		if ($this->isPost ()) {
			$Admin = D ( "Admin" );
			$data ['username'] = $this->_post ( "username" );
			if (empty ( $data ['username'] )) {
				$this->error ( "用户名不能为空!" );
			}
			
			$password = $this->_post ( "password" );
			$data ['password'] = $this->_post ( "password" );
			if (empty ( $data ['password'] )) {
				$this->error ( "密码不能为空!" );
			}
			$pwdconfirm = $this->_post ( "pwdconfirm" );
			$data ['pwdconfirm'] = $this->_post ( "pwdconfirm" );
			if (empty ( $data ['pwdconfirm'] )) {
				$this->error ( "确认密码不能为空!" );
			}
			
			$email = $this->_post ( "email" );
			$data ['email'] = $this->_post ( "email" );
			if (empty ( $data ['email'] )) {
				$this->error ( "E-mail不能为空!" );
			}
			$realname = $this->_post ( "realname" );
			$data ['realname'] = $this->_post ( "realname" );
			if (empty ( $data ['realname'] )) {
				$this->error ( "真实姓名不能为空!" );
			}
			$roleid = $this->_post ( "roleid" );
			$data ['roleid'] = $this->_post ( "roleid" );
			if (empty ( $data ['roleid'] )) {
				$this->error ( "所属角色不能为空!" );
			}
			
			$data ['lastloginip'] = get_client_ip ();
			$data ['lastlogintime'] = time ();
			$Admin->create ( $data );
			$return = $Admin->add ();
			
			if ($return) {
				$this->success ( "添加管理员成功!", "/administrator/" );
			} else {
				$this->error ( "添加管理员失败!" );
			}
		} else {
			$Admin = D ( "Admin" );
			
			$AdminRole = D ( "AdminRole" );
			$role_list = $AdminRole->order ( "listorder ASC, roleid ASC" )->select ();
			
			$this->assign ( 'role_list', $role_list );
			$this->display ();
		}
	}
	
	/**
	 * 编辑
	 */
	public function edit() {
		
		if ($this->isPost ()) {
			$uid = $this->_post ( 'uid' );
			
			$Admin = D ( "Admin" );
			
			$password = $this->_post ( "password" );
			$pwdconfirm = $this->_post ( "pwdconfirm" );
			
			if (! empty ( $password ) && ! empty ( $pwdconfirm )) {
				
				if ($password != $pwdconfirm) {
					$msg = array (
							'rtn_code' => - 1,
							'content' => '两次输入密码不一样!' 
					);
					echo json_encode ( $msg );
					exit ();
				}
				
				$data ['password'] = md5 ( $password );
			}
			
			$data ['roleid'] = $this->_post ( "roleid" );
			if (empty ( $data ['roleid'] )) {
				$msg = array (
						'rtn_code' => - 1,
						'content' => '所属角色不能为空!' 
				);
				echo json_encode ( $msg );
				exit ();
			}
			$data ['email'] = $this->_post ( "email" );
			if (empty ( $data ['email'] )) {
				$msg = array (
						'rtn_code' => - 1,
						'content' => 'E-mail不能为空!' 
				);
				echo json_encode ( $msg );
				exit ();
			}
			$data ['realname'] = $this->_post ( "realname" );
			if (empty ( $data ['realname'] )) {
				$msg = array (
						'rtn_code' => - 1,
						'content' => '真实姓名不能为空!' 
				);
				echo json_encode ( $msg );
				exit ();
			}
			
			$return = $Admin->where ( "uid={$uid}" )->data ( $data )->save ();
			
			if ($return) {
				$msg = array (
						'rtn_code' => 0,
						'content' => '编辑管理员成功!' 
				);
				echo json_encode ( $msg );
				exit ();
			} else {
				$msg = array (
						'rtn_code' => 0,
						'content' => '编辑管理员失败!' 
				);
				echo json_encode ( $msg );
				exit ();
			}
		} else {
			$uid = $this->_get ( 'uid' );
			
			$Admin = D ( "Admin" );
			$Admin_Info = $Admin->find ( $uid );
			
			$AdminRole = D ( "AdminRole" );
			$role_list = $AdminRole->order ( "listorder ASC, roleid ASC" )->select ();
			if ($role_list) {
				foreach ( $role_list as &$val ) {
					if ($val ['roleid'] == $Admin_Info ['roleid']) {
						$val ['is_selected'] = 1;
					} else {
						$val ['is_selected'] = 0;
					}
				}
			}
			$this->assign ( 'role_list', $role_list );
			
			$this->assign ( 'Admin_Info', $Admin_Info );
			$this->display ();
		}
	}
	
	/**
	 * 删除
	 */
	public function delete() {
	
	}
}
?>